<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{Banner, Img, User, LoveBlack, LoveMarry};
use app\common\validate\{BannerValidate, BlackValidate, LoveMarryValidate};

class HomeController extends BaseController
{
    /** 查询轮播图列表 */
    public function banner_list()
    {
        $list = Banner::field('id, img')->select();
        $list->isEmpty()?$this->error_back('暂无数据'):$this->success_back('ok', $list);
    }

    /** 上传图片 */
    public function img_up()
    {
        $data = input('');
        $data['img'] = request()->file('img');

        try{
            validate(BannerValidate::class)->scene('img')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $path = $data['path'];

        if(isset($data['obj_id']) && !empty($data['obj_id'])){
            $path = $data['path'] . '/' . $data['obj_id'];
        }

        $img = fileUp($data['img'], $path);

        $this->success_back('上传成功', $img);
    }

    /** 情场黑名单录入信息 */
    public function set_love_black()
    {
        $data = input('');

        try{
            validate(BlackValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $blackModel = new LoveBlack();

        /** 录入信息 */
        $flag = $blackModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $imgModel = new Img();
        $img_add_arr = [];

        /** 对方照片 */
        if(isset($data['to_user_img']) && !empty($data['to_user_img'])){
            $to_user_img = json_decode($data['to_user_img']);
            
            foreach($to_user_img as $v){
                $to_user_img_info = [
                    'obj_id'   => $flag['data'],
                    'obj_type' => 1,
                    'type'     => 3,
                    'img'      => $v,
                ];

                $img_add_arr[] = $to_user_img_info;
            }
        }

        /** 佐证资料 */
        if(isset($data['proof_img']) && !empty($data['proof_img'])){
            $proof_img = json_decode($data['proof_img']);
            
            foreach($proof_img as $v){
                $proof_img_info = [
                    'obj_id' => $flag['data'],
                    'obj_type' => 2,
                    'type'     => 3,
                    'img'    => $v,
                ];

                $img_add_arr[] = $proof_img_info;
            }
        }

        if($img_add_arr){
            /** 批量新增图片 */
            $imgModel->addAll($img_add_arr);
        }

        $this->success_back('添加成功');
    }

    /** 情场黑名单查询 */
    public function love_black_list()
    {
        $data = input('');

        try{
            validate(BlackValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $imgModel = new Img();
        $userModel = new User();
        $blackModel = new LoveBlack();

        /** 查询信息 */
        $field = ['id', 'user_id', 'real_name', 'card_id', 'relation', 'along_time_start', 'along_time_end', 'remarks'];
        $list = $blackModel->getList(['card_id' => $data['card_id']], 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        foreach($list['data'] as $v){
            $v['nickname']   = $userModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
            $v['along_time'] = $v['along_time_start'] . '-' . $v['along_time_end'];
            $proof_imgs      = $imgModel->getList(['obj_id' => $v['id'], 'type' => 3, 'obj_type' => 2], 0, 0, 0, 'img');

            if($proof_imgs['code'] != 'ok'){
                $proof_img = [];
            }else{
                $proof_img_arr = $proof_imgs['data']->toArray();
                $proof_img = array_column($proof_img_arr, 'img');
            }

            $v['proof_img'] = $proof_img;

            unset($v['along_time_start']);
            unset($v['along_time_end']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 婚姻查询录入信息 */
    public function set_love_marry()
    {
        $data = input('');

        try{
            validate(LoveMarryValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $marryModel = new LoveMarry();

        /** 录入信息 */
        $flag = $marryModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $imgModel = new Img();
        $img_add_arr = [];

        /** 对方照片 */
        if(isset($data['to_user_img']) && !empty($data['to_user_img'])){
            $to_user_img = json_decode($data['to_user_img']);
            
            foreach($to_user_img as $v){
                $to_user_img_info = [
                    'obj_id'   => $flag['data'],
                    'obj_type' => 1,
                    'type'     => 4,
                    'img'      => $v,
                ];

                $img_add_arr[] = $to_user_img_info;
            }
        }

        /** 佐证资料 */
        if(isset($data['proof_img']) && !empty($data['proof_img'])){
            $proof_img = json_decode($data['proof_img']);
            
            foreach($proof_img as $v){
                $proof_img_info = [
                    'obj_id' => $flag['data'],
                    'obj_type' => 2,
                    'type'     => 4,
                    'img'    => $v,
                ];

                $img_add_arr[] = $proof_img_info;
            }
        }

        if($img_add_arr){
            /** 批量新增图片 */
            $imgModel->addAll($img_add_arr);
        }

        $this->success_back('添加成功');
    }

    /** 婚姻查询 */
    public function love_marry_list()
    {
        $data = input('');

        try{
            validate(LoveMarryValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $imgModel = new Img();
        $userModel = new User();
        $marryModel = new LoveMarry();

        /** 查询信息 */
        $field = ['id', 'user_id', 'real_name', 'card_id', 'relation', 'marry_status', 'remarks'];
        $list = $marryModel->getList(['card_id' => $data['card_id']], 1, $data['page'], $data['limit'], $field);

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        foreach($list['data'] as &$v){
            $proof_imgs = $imgModel->getList(['obj_id' => $v['id'], 'type' => 4, 'obj_type' => 2], 0, 0, 0, 'img');

            if($proof_imgs['code'] != 'ok'){
                $proof_img = [];
            }else{
                $proof_img_arr = $proof_imgs['data']->toArray();
                $proof_img = array_column($proof_img_arr, 'img');
            }

            $v['nickname']  = $userModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
            $v['proof_img'] = $proof_img;
        }

        $this->success_back('ok', $list['data']);
    }
}
